package soccer.view;

import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;

public class CoachSearchPanel extends JPanel {
	private JTextField SearchByNametextField;
	private JTextField SearchBySurnametextField;
	private JPanel searchByNamepanel;
	private JLabel lblNameLabel;
	private JLabel lblSurnameLabel;
	private JPanel searchByTeampanel;
	private JTable table;
	private JLabel lblSearchResultLabel;
	private JScrollPane scrollResultPane;
	private JButton btnSearchButton;
	private JRadioButton rdbtnSearchByName;
	private JRadioButton rdbtnSearchByTeam;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JComboBox comboBox;

	/**
	 * Create the panel.
	 */
	public CoachSearchPanel() {
		setLayout(null);
		
		searchByNamepanel = new JPanel();
		searchByNamepanel.setBounds(10, 101, 275, 85);
		add(searchByNamepanel);
		searchByNamepanel.setLayout(null);
		
		SearchByNametextField = new JTextField();
		SearchByNametextField.setBounds(60, 11, 86, 20);
		searchByNamepanel.add(SearchByNametextField);
		SearchByNametextField.setColumns(10);
		
		SearchBySurnametextField = new JTextField();
		SearchBySurnametextField.setEnabled(true);
		SearchBySurnametextField.setEditable(true);
		SearchBySurnametextField.setText("");
		SearchBySurnametextField.setBounds(60, 42, 86, 20);
		searchByNamepanel.add(SearchBySurnametextField);
		SearchBySurnametextField.setColumns(10);
		
		lblNameLabel = new JLabel("Name");
		lblNameLabel.setBounds(10, 14, 46, 14);
		searchByNamepanel.add(lblNameLabel);
		
		lblSurnameLabel = new JLabel("Surname");
		lblSurnameLabel.setBounds(10, 45, 46, 14);
		searchByNamepanel.add(lblSurnameLabel);
		
		rdbtnSearchByName = new JRadioButton("Search By Name");
		buttonGroup.add(rdbtnSearchByName);
		rdbtnSearchByName.setSelected(true);
		rdbtnSearchByName.setBounds(10, 44, 109, 23);
		rdbtnSearchByName.setActionCommand("byName");
		add(rdbtnSearchByName);
		
		rdbtnSearchByTeam = new JRadioButton("Search By Team");
		buttonGroup.add(rdbtnSearchByTeam);
		rdbtnSearchByTeam.setBounds(130, 44, 109, 23);
		rdbtnSearchByTeam.setActionCommand("byTeam");
		add(rdbtnSearchByTeam);
	    
		
		lblSearchResultLabel = new JLabel("Search Result");
		lblSearchResultLabel.setBounds(10, 224, 78, 14);
		add(lblSearchResultLabel);
		
		scrollResultPane = new JScrollPane();
		scrollResultPane.setBounds(10, 238, 549, 92);
		add(scrollResultPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
			},
			new String[] {
				"AgreementBegin", "AgrementFinish", "Coach Name", "Coach Surname", "Birthdate", "Birthplace"
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(92);
		table.getColumnModel().getColumn(1).setPreferredWidth(90);
		table.getColumnModel().getColumn(3).setPreferredWidth(91);
		scrollResultPane.setColumnHeaderView(table);
		scrollResultPane.setViewportView(table);
		
		btnSearchButton = new JButton("Search");
		btnSearchButton.setBounds(10, 197, 89, 23);
		add(btnSearchButton);
		
		searchByTeampanel = new JPanel();
		searchByTeampanel.setBounds(10, 101, 275, 85);
		add(searchByTeampanel);
		searchByTeampanel.setLayout(null);
		
		comboBox = new JComboBox();
		comboBox.setBounds(0, 0, 89, 20);
		searchByTeampanel.add(comboBox);
		comboBox.setVisible(false);
	}
	 
	

	public ButtonGroup getButtonGroup() {
		return buttonGroup;
	}



	public JComboBox getComboBox() {
		return comboBox;
	}



	public void setComboBox(JComboBox comboBox) {
		this.comboBox = comboBox;
	}

	public void fillComboBoxWithTeams(List<Map> teamsName){
	    Object[] items = new Object[teamsName.size()];
	     
	    for(int i = 0; i < items.length; i++){
	    	Map item = teamsName.get(i);
	    	items[i] = item.get("teamName");
	    }
	    DefaultComboBoxModel model = new DefaultComboBoxModel(items);
		comboBox.setModel(model);
    }
	public  Object getSelectedTeamFromComboBox(){
		Object selectedItem = comboBox.getSelectedItem();
		return selectedItem;
	}


	public void addCoachSpecialitiesToTable(List coachList){
		 if(coachList==null || coachList.size()<1){
			 lblSearchResultLabel.setText(" * The searched coach couldn't be found.");
			 scrollResultPane.setVisible(false);
			 lblSearchResultLabel.setVisible(true);
		 }
		 DefaultTableModel tableModel= (DefaultTableModel)table.getModel() ;
		 resetCoachTable(tableModel);
		 
		 for(int i=0;i<coachList.size();i++){
			 Vector<Comparable> v=new Vector<Comparable>();
			 Map coach=(Map)coachList.get(i);
			 v.add((Comparable) coach.get("agreementBegin"));
			 v.add((Comparable) coach.get("agreementFinish"));
			 v.add((Comparable) coach.get("name"));
			 v.add((Comparable) coach.get("surname"));
			 v.add((Comparable) coach.get("birthdate"));
			 v.add((Comparable) coach.get("birthplace"));
			 tableModel.addRow(v);
		 }
		 lblSearchResultLabel.setVisible(false);
		 scrollResultPane.setVisible(true);
	 }
	 public void resetCoachTable(DefaultTableModel model){
			while(model.getRowCount() > 0){
				model.removeRow(0);
			}
		}
	 public void setVisibleSearchNamePanel(){
		 searchByNamepanel.setVisible(true);
		 searchByTeampanel.setVisible(false);
		 
		
	 }
	 public void setVisibleSearchTeamPanel(){
		 searchByNamepanel.setVisible(false);
		 searchByTeampanel.setVisible(true);
		
	 }
	 public void addSearchResultButtonActionListener(ActionListener l){
		 btnSearchButton.addActionListener(l);
	 }
	 
	 public void addSearchByNameButtonActionListener(ActionListener l){
	    rdbtnSearchByName.addActionListener(l);
	    }
	 
	 public void addSearchByTeamButtonActionListener(ActionListener l){
		    rdbtnSearchByTeam.addActionListener(l);
		    }
		 
	public JTextField getSearchByNametextField() {
		return SearchByNametextField;
	}

	
	public JRadioButton getRdbtnSearchByName() {
		return rdbtnSearchByName;
	}



	public void setRdbtnSearchByName(JRadioButton rdbtnSearchByName) {
		this.rdbtnSearchByName = rdbtnSearchByName;
	}



	public JRadioButton getRdbtnSearchByTeam() {
		return rdbtnSearchByTeam;
	}



	public void setRdbtnSearchByTeam(JRadioButton rdbtnSearchByTeam) {
		this.rdbtnSearchByTeam = rdbtnSearchByTeam;
	}



	public void setSearchByNametextField(JTextField searchByNametextField) {
		SearchByNametextField = searchByNametextField;
	}


	public JTextField getSearchBySurnametextField() {
		return SearchBySurnametextField;
	}


	public void setSearchBySurnametextField(JTextField searchBySurnametextField) {
		SearchBySurnametextField = searchBySurnametextField;
	}


	public JPanel getSearchByNamepanel() {
		return searchByNamepanel;
	}


	public void setSearchByNamepanel(JPanel searchByNamepanel) {
		this.searchByNamepanel = searchByNamepanel;
	}


	public JLabel getLblNameLabel() {
		return lblNameLabel;
	}


	public void setLblNameLabel(JLabel lblNameLabel) {
		this.lblNameLabel = lblNameLabel;
	}


	public JLabel getLblSurnameLabel() {
		return lblSurnameLabel;
	}


	public void setLblSurnameLabel(JLabel lblSurnameLabel) {
		this.lblSurnameLabel = lblSurnameLabel;
	}


	public JPanel getSearchByTeampanel() {
		return searchByTeampanel;
	}


	public void setSearchByTeampanel(JPanel searchByTeampanel) {
		this.searchByTeampanel = searchByTeampanel;
	}




	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}

	public JLabel getLblSearchResultLabel() {
		return lblSearchResultLabel;
	}

	public void setLblSearchResultLabel(JLabel lblSearchResultLabel) {
		this.lblSearchResultLabel = lblSearchResultLabel;
	}

	public JScrollPane getScrollResultPane() {
		return scrollResultPane;
	}

	public void setScrollResultPane(JScrollPane scrollResultPane) {
		this.scrollResultPane = scrollResultPane;
	}

	public JButton getBtnSearchButton() {
		return btnSearchButton;
	}

	public void setBtnSearchButton(JButton btnSearchButton) {
		this.btnSearchButton = btnSearchButton;
	}
	}
